//
//  KLHomeViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/25.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLHomeViewController.h"
#import "STAdvertingScrollView.h"
#import "KLIdeSellViewController.h"
#import "KLIdeRecomendViewController.h"
#import "KLBasketViewController.h"
#import "KLStayViewController.h"
#import "KLTodayGoodViewController.h"
#import "KLCarSaleViewController.h"
#import "KLEspeciallyViewController.h"
#import "KLNearByViewController.h"
#import "STLocationManger.h"
#import "KLNormalSpeedViewController.h"
#import "STAreaViewController.h"
#import "KLHomeBannerModel.h"
#import "STMenuControl.h"
#import "KLHomePresentViewController.h"
#import "KLDoctorListViewController.h"
#import "KLWeekListViewController.h"
#import "KLCarMessageViewController.h"
#import "KLStayViewController.h"
#import "KLCarSaleViewController.h"
@interface KLHomeViewController ()
@property(nonatomic, strong)  STAdvertingScrollView     *adverView;
@property(nonatomic, strong)  STAreaViewController    *areaVC;
@end

@implementation KLHomeViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNav];
    [self configSubView];
    [self sendBanerRequest];
//    [[STLocationManger defult] startUpdatingLocationHande:^(STError *error, CLPlacemark *placemark) {
//        if (!error && placemark) {
//            [[STLocationManger defult] updatePlacemarkWith:placemark];
//            [self st_setLeftItemWithImage:[UIImage imageNamed:@"icon_定位"]
//                             andwithTitle:placemark.locality
//                               titleColor:[UIColor whiteColor]];
//            self.areaVC.locationCityStr = placemark.locality;
//        }
//        else{
//            [SVProgressHUD showErrorWithStatus:error.desc];
//        }
//    }];

    NSString * leftTitle = @"请定位";
    if ([STUserManger defult].chnelPalce.length) {
         NSString * lastPalce = [[STUserManger defult].chnelPalce componentsSeparatedByString:@"-"].lastObject;
        leftTitle = lastPalce;
    }
    [self st_setLeftItemWithImage:[UIImage imageNamed:@"icon_定位"]
                     andwithTitle:leftTitle
                       titleColor:[UIColor whiteColor]];
    //获取七牛token
    [self fetch_QiNiu_TokenRequest];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendBanerRequest];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)configNav{
    UIImageView * titleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 69, 20)];
    titleView.image = [UIImage imageNamed:@"昆仑科技"];
    self.navigationItem.titleView = titleView;
    NSString * title = [[STLocationManger defult] lastCity];
    [self st_setLeftItemWithImage:[UIImage imageNamed:@"icon_定位"]
                     andwithTitle:title.length?title:@""
                       titleColor:[UIColor whiteColor]];
    
    self.areaVC = [STAreaViewController new];
}
#pragma mark --subView
- (void)configSubView{
    self.tableView.height = self.tableView.height ;
    self.tableView.scrollEnabled = NO;
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
//    self.adverView = [[STAdvertingScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150) andWithArray:@[@"banner",@"banner",@"banner",@"banner",@"banner"] handle:^(NSInteger num) {
//
//    }];
//    self.adverView.pageControll.bottom = self.adverView.height - 15;
//    self.adverView.pageControll.currentPageIndicatorTintColor = [UIColor whiteColor];
//    self.adverView.pageControll.pageIndicatorTintColor = KL_BlueBackGroundColor;
//    self.adverView.pageControll.width = self.adverView.dataSouce.count * 10;
//    self.adverView.pageControll.centerX = UIScreenWidth / 2;
//    self.adverView.canAutoScroll = YES;
//    self.adverView.time = 3;
//    [headerView addSubview:self.adverView];
    //底部
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight - 64 -  49)];
    imageView.image = [UIImage imageNamed:@"组10"];
    imageView.userInteractionEnabled = NO;
    [headerView addSubview:imageView];
    
    UIImageView * roudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 353, 353)];
    roudImageView.image = [UIImage imageNamed:@"ԲԲ"];
    roudImageView.userInteractionEnabled = NO;
    roudImageView.centerX = UIScreenWidth / 2;
    [imageView addSubview:roudImageView];
    

    //按钮
    __weak typeof(self) weakSelf =  self;
   
 
    UIView * speedView = [self viewWithFrame:CGRectMake(0, 0, 120, 60) imageName:@"云加速" subImageName:@"匀加速"];
    speedView.top =roudImageView.top +  40;
    speedView.centerX = roudImageView.centerX;
    speedView.tag = 10000;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                           action:@selector(onSlectedBigButton)];
    [speedView addGestureRecognizer:tap];
    [headerView addSubview:speedView];
    
    UIView * blankimageView = [self blackView];
    blankimageView.left = roudImageView.left + 40;
    blankimageView.centerY = roudImageView.centerY;
    blankimageView.tag = 10001;
    blankimageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                           action:@selector(onSlectedCaiYuanZiButton)];
    [blankimageView addGestureRecognizer:tap1];
    [headerView addSubview:blankimageView];
    
    UIView * speciallyView = [self viewWithFrame:CGRectMake(0, 0, 120, 60) imageName:@"云加速" subImageName:@"地方特产"];
    speciallyView.bottom = roudImageView.bottom   - 60;
    speciallyView.centerX = roudImageView.centerX;
    speciallyView.tag = 10002;
    speciallyView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(onSlectedGeDiButton)];
    [speciallyView addGestureRecognizer:tap2];
    [headerView addSubview:speciallyView];
    
    UIView * todayView = [self viewWithFrame:CGRectMake(0, 0, 120, 60) imageName:@"云加速" subImageName:@"今日特价"];
    todayView.centerY = roudImageView.centerY - 10;
    todayView.right = roudImageView.right -  20;
    todayView.tag = 10003;
    todayView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(onSlectedJinriButton)];
    [todayView addGestureRecognizer:tap4];
    [headerView addSubview:todayView];
    
    //更多
    STButton * moreButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"更多拷贝"]];
    moreButton.center = roudImageView.center;
    [moreButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedMoreButton];
    }];
    moreButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [headerView addSubview:moreButton];
    headerView.height = imageView.bottom;
    self.tableView.tableHeaderView = headerView;
}
- (UIView*)blackView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 80)];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 31,68)];
    imageView.image = [UIImage imageNamed:@"菜园子"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.centerY = view.height / 2;
    imageView.left = 0;
    imageView.userInteractionEnabled = NO;
    [view addSubview:imageView];
    
    UIImageView * subimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 31,68)];
    subimageView.image = [UIImage imageNamed:@"菜篮子"];
    subimageView.contentMode = UIViewContentModeScaleAspectFit;
    subimageView.centerY = view.height / 2;
    subimageView.right = view.width;
    subimageView.userInteractionEnabled = NO;
    [view addSubview:subimageView];

    return view;
}
- (UIView*)viewWithFrame:(CGRect)frame imageName:(NSString*)imageName subImageName:(NSString*)subImageName{
    UIView * view = [[UIView alloc] initWithFrame:frame];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60,60)];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.top = 0;
    imageView.centerX = view.width / 2;
    imageView.userInteractionEnabled = NO;
    [view addSubview:imageView];
    
    UIImageView * subimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.width,40)];
    subimageView.image = [UIImage imageNamed:subImageName];
    subimageView.contentMode = UIViewContentModeScaleAspectFit;
    subimageView.top = imageView.bottom - 10 ;
    subimageView.centerX = view.width / 2;
    subimageView.userInteractionEnabled = NO;
    [view addSubview:subimageView];


    return view;
}
#pragma mark --Action Method
- (void)st_leftBarAction:(id)sender{
    __weak typeof(self) weakSelf =  self;
//    [self.areaVC setBlock:^(NSString *chosedTitle) {
//        [weakSelf st_setLeftItemWithImage:[UIImage imageNamed:@"icon_定位"]
//                         andwithTitle:chosedTitle
//                           titleColor:[UIColor whiteColor]];
//    }];
//    STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:self.areaVC];
//    [self presentViewController:nav animated:YES completion:nil];
    KLChannelViewController * vc = [KLChannelViewController new];
    [vc setDidChoseArea:^(NSString *places, NSString *codes) {

        //NSString * lastCode = [codes componentsSeparatedByString:@"-"].lastObject;
        NSString * lastPalce = [places componentsSeparatedByString:@"-"].lastObject;
        [STUserManger defult].code = codes;
        [STUserManger defult].chnelPalce = places;
        [weakSelf st_setLeftItemWithImage:[UIImage imageNamed:@"icon_定位"]
                             andwithTitle:lastPalce
                               titleColor:[UIColor whiteColor]];
    }];
    [self.navigationController pushViewController:vc animated:YES];
    

}
//平台自营
- (void)onSlectedPingtaiButton{
    
    [self.navigationController pushViewController:[KLIdeSellViewController new] animated:YES];
}
//平台推荐
- (void)onSlectedPingtaiTuiJianButton{
    [self.navigationController pushViewController:[KLIdeRecomendViewController new] animated:YES];
}
//平台菜园子
- (void)onSlectedCaiYuanZiButton{
    [self.navigationController pushViewController:[KLBasketViewController new] animated:YES];
}
//住宿
- (void)onSlectedZhuSuButton{
    [self.navigationController pushViewController:[KLStayViewController new] animated:YES];
}
//今日特价
- (void)onSlectedJinriButton{
    [self.navigationController pushViewController:[KLTodayGoodViewController new] animated:YES];
}
//名车出租
- (void)onSlectedMingcheButton{
    [self.navigationController pushViewController:[KLCarSaleViewController new] animated:YES];
}
//各地特产
- (void)onSlectedGeDiButton{
    
    [self.navigationController pushViewController:[KLEspeciallyViewController new] animated:YES];
}
//附近商家
- (void)onSlectedFuJinButton{
    [self.navigationController pushViewController:[KLNearByViewController new] animated:YES];
}
//匀加速折扣
- (void)onSlectedBigButton{
    
    [self.navigationController pushViewController:[KLNormalSpeedViewController new] animated:YES];
}
- (void)onTapIconImageView:(UITapGestureRecognizer*)ges{
    if (ges.view.tag == 10000) {
        [self onSlectedBigButton];
    }
    if (ges.view.tag == 10001) {
        [self onSlectedCaiYuanZiButton];
    }
    if (ges.view.tag == 10002) {
         [self onSlectedGeDiButton];
    }
    if (ges.view.tag == 10003) {
         [self onSlectedJinriButton];
    }
}
//更多按钮
- (void)onSelctedMoreButton{
    KLHomePresentViewController * vc = [[KLHomePresentViewController alloc] initWithHandle:^(NSString *title) {
        NSLog(@"选择了%@",title);
        if ([title isEqualToString:@"良医治病"]) {
            [self.navigationController pushViewController:[KLDoctorListViewController new] animated:YES];
        }
        if ([title isEqualToString:@"患者求医"]) {
            [self.navigationController pushViewController:[KLWeekListViewController new] animated:YES];
        }
        if ([title isEqualToString:@"货运信息"]) {
            [self.navigationController pushViewController:[KLCarMessageViewController new] animated:YES];
        }
       
        if ([title isEqualToString:@"民车出租"]) {
            [self.navigationController pushViewController:[KLCarSaleViewController new] animated:YES];
        }
        
        if ([title isEqualToString:@"酒店住宿"]) {
            [self.navigationController pushViewController:[KLStayViewController new] animated:YES];
        }
        if ([title isEqualToString:@"饭店订桌"]) {
            [SVProgressHUD showInfoWithStatus:@"该功能正在完善"];
        }
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --NetWork Method
- (void)sendBanerRequest{
      NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/index"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:nil
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                
                                                 NSDictionary * dic = responseObject[@"data"];
                                                 if ([dic.allKeys containsObject:@"banner"]) {
                                                     NSArray * banner = dic[@"banner"];
                                                     NSMutableArray * arrays = [NSMutableArray new];
                                                     for (NSDictionary * bannerDic in banner) {
                                                         KLHomeBannerModel * model = [KLHomeBannerModel mj_objectWithKeyValues:bannerDic];
                                                         [arrays addObject:model.banner_img];
                                                     }
                                                     self.adverView.imageUrlsArray = arrays;
                                                 }
                                                 
                                                 //映射关系
                                                 if ([dic.allKeys containsObject:@"classify"]) {
                                                     [KLClassify defult].classify = dic[@"classify"];
                                                     
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)fetch_QiNiu_TokenRequest{
    if ([STUserManger defult].token.length) {
        NSMutableDictionary * dic = [NSMutableDictionary new];
        NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/upload",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
        [[STNetWrokManger defaultClient] requestWithPath:url
                                                  method:STHttpRequestTypePost
                                              parameters:dic
                                                 success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                     
                                                     NSDictionary * dataDic =  responseObject[@"data"];
                                                     if ([dataDic.allKeys containsObject:@"upload_tk"]) {
                                                         [STUserManger defult].upload_tk = dataDic[@"upload_tk"];
                                                     }
                                                     

                                                 } failure:^(NSString *stateCode, STError *error) {
                                                     if (![STUserManger defult].upload_tk.length) {
                                                         [self fetch_QiNiu_TokenRequest];
                                                     }
                                                 }];
    }
}
@end

